clear
set more off
set varabbrev off
set niceness 6, permanently

/* Define globals */
global workingDir
global dataDir
global CDWDir

capture log close
log using "$workingDir/log/PTC_by_FPL.log", replace


* Merge f1095As with f8962s
do "$workingDir/code/merge_8962_1095_nf_data.do"


use "$workingDir/temp/mergedData.dta", clear
replace f8962 = 0 if f8962 == .
replace irmf = 0 if irmf == .
replace filer = 0 if filer == .
replace timelyFiler = 0 if timelyFiler == .
replace neverFiler = 0 if neverFiler == .
keep if f8962 == 1 | irmf == 1 | neverFiler == 1
drop if dsi == 1
gen nonRecon = (f8962 == 0 & filer == 1)
capture drop temp
bysort flpdyr recid: gen temp = _N
bysort flpdyr recid: egen temp_latest = max(soiYr)
gen temp_keep = 1 if temp == 1
replace temp_keep = 1 if (temp_latest == soiYr) & (temp > 1)
replace temp_keep = 0 if temp_keep == .
drop if temp_keep == 0
drop temp*

* Assign artificial recid to neverFilers from 2014/20/21 who don't have one from SOI/CDW
bysort flpdyr: egen temp = max(recid)
bysort flpdyr recid: replace recid = temp + _n if recid == .
drop temp*

* Should affect zero observations
capture drop temp
bysort flpdyr recid: gen temp = _n
drop if temp > 1
drop temp

replace annAPTC_f1095A = aptc_13 if annAPTC_f1095A == . & cwhs_nf == 1
replace famSize = famsize if famSize == . & cwhs_nf == 1
replace stateExchID = substr(exchange_id, 4, 2) if stateExchID == "" & cwhs_nf == 1

* No APTC and no reconciliation, so no PTC
drop if annAPTC_f1095A == 0 & f8962 == 0

replace famSize = famSize_f1095A if famSize == .
drop if famSize == 0

foreach x of varlist issn s003 s025 s026 s027 s028 s029 s030 s031 s032 s033 s034 {
	replace `x' = 0 if `x' == .
}
replace stateFPL = 2 if stateFPL == . & stateExchID == "AK"
replace stateFPL = 3 if stateFPL == . & stateExchID == "HI"
replace stateFPL = 1 if stateFPL == .

* Merge in poverty and PTC calculation parameters
gen tempFamSize = famSize
replace famSize = 8 if famSize > 8
replace flpdyr = flpdyr - 1
capture drop _merge
merge m:1 flpdyr stateFPL famSize using "$dataDir/fpl.dta"
replace flpdyr = flpdyr + 1
keep if _merge == 3
capture drop temp
gen temp = tempFamSize - famSize
replace FPL = FPL + temp * FPLAdd if temp > 0
drop famSize FPLAdd temp _merge
rename tempFamSize famSize

capture drop _merge
merge m:1 flpdyr using "$dataDir/af.dta"
keep if _merge == 3
drop _merge

* Merge in repayment limitation amounts
capture drop _merge
gen singfil = (mars == 1)
merge m:1 flpdyr singfil using "$dataDir/repay.dta"
keep if _merge == 3
drop _merge singfil

* Clean up incomes
replace agi_kid_file = 0 if agi_kid_file == .
gen exch_income = exch_income_f8962
replace exch_income = (totinc - ttadj) + (ssinc - ssagi) + forexc + texint + agi_kid_file if exch_income == .
replace exch_income = (totinc - ttadj) + (ssinc - ssagi) + forexc + texint + agi_kid_file if filer == 1 & f8962 == 0
replace exch_inc = 0 if exch_inc == . & neverFiler == 1
replace pctfpl = 0 if exch_inc == 0 & neverFiler == 1
replace exch_income = exch_inc if neverFiler == 1

gen calcFPL = round(exch_income / FPL, 0.01)
replace calcFPL = pctfpl if neverFiler == 1 & pctfpl != .
replace calcFPL = 0 if calcFPL < 0

* Fix 2020 UI safe harbor recapture
rename e02300 ui_comp
replace ui_comp = 0 if ui_comp == .
replace ui = 0 if ui == .
gen i_ui = (ui_comp > 0)
replace i_ui = ui if ui > 0
replace calcFPL = 1.33 if i_ui > 0 & flpdyr == 2020
replace hincpl = calcFPL*100 if nonRecon == 1 | neverFiler == 1

gen temp_type = 0
replace temp_type = 1 if nonRecon == 1
replace temp_type = 2 if neverFiler == 1

* For flpdyr < 2021, "standard" schedule
gen payout_max = 1.0
replace payout_max = AFLT133 if calcFPL < 1.33 & flpdyr < 2021
replace payout_max = AF133 + (calcFPL-1.33) * ((AF150 - AF133)/0.17) if calcFPL >= 1.33 & calcFPL < 1.50 & flpdyr < 2021
replace payout_max = AF150 + (calcFPL-1.5) * ((AF200 - AF150)/0.5) if calcFPL >= 1.50 & calcFPL < 2.00 & flpdyr < 2021
replace payout_max = AF200 + (calcFPL-2.0) * ((AF250 - AF200)/0.5) if calcFPL >= 2.00 & calcFPL < 2.50 & flpdyr < 2021
replace payout_max = AF250 + (calcFPL-2.5) * ((AF300 - AF250)/0.5) if calcFPL >= 2.50 & calcFPL < 3.00 & flpdyr < 2021
replace payout_max = AF300 if calcFPL >= 3.00 & calcFPL <= 4.00 & flpdyr < 2021

* For flpdyr > 2020, enhanced PTC schedule
replace payout_max = AF150 if calcFPL < 1.5 & flpdyr > 2020
replace payout_max = AF150 + (calcFPL-1.5) * ((AF200 - AF150)/0.5) if calcFPL >= 1.50 & calcFPL < 2.00 & flpdyr > 2020
replace payout_max = AF200 + (calcFPL-2.0) * ((AF250 - AF200)/0.5) if calcFPL >= 2.00 & calcFPL < 2.50 & flpdyr > 2020
replace payout_max = AF250 + (calcFPL-2.5) * ((AF300 - AF250)/0.5) if calcFPL >= 2.50 & calcFPL < 3.00 & flpdyr > 2020
replace payout_max = AF300 + (calcFPL-3.0) * ((AFGT400 - AF300)/1.0) if calcFPL >= 3.00 & calcFPL < 4.00 & flpdyr > 2020
replace payout_max = AFGT400 if calcFPL >= 4.00 & flpdyr > 2020

* Calc repayment limitation == . for those with calcFPL > 4.0
gen calc_repayLim = .
replace calc_repayLim = repayLT200 if round(calcFPL, 0.01) < 2.0 & calc_repayLim == .
replace calc_repayLim = repayLT300 if round(calcFPL, 0.01) < 3.0 & calc_repayLim == .
replace calc_repayLim = repayLT400 if round(calcFPL, 0.01) < 4.0 & calc_repayLim == .

* Fill in missing reconciliation information for filer non-reconcilers
gen tempAnnPrem = annPrem_f1095A if nonRecon == 1 | neverFiler == 1
gen tempAnnSLCSP = annSLCSP_f1095A if nonRecon == 1 | neverFiler == 1
gen tempAnnAPTC = annAPTC_f1095A if nonRecon == 1 | neverFiler == 1

* For non-reconcilers that were supplemented using CDW data, use Lin's variable
replace tempAnnPrem = prem_13 if nonRecon == 1 & tempAnnPrem == .
replace tempAnnSLCSP = slcsp_13 if nonRecon == 1 & tempAnnSLCSP == .
replace tempAnnAPTC = aptc_13 if nonRecon == 1 & tempAnnAPTC == .

* Calculate PTC components for filer-non-reconcilers 
gen maxOOPPrem = payout_max * exch_income if exch_income > 0
gen tempPTC = min(tempAnnPrem, max(tempAnnSLCSP - maxOOPPrem, 0))
gen tempTrueUp = max(tempPTC - tempAnnAPTC, 0)
gen tempExcessAPTC = max(tempAnnAPTC - tempPTC, 0)
gen tempAPTC_repay = min(tempExcessAPTC, calc_repayLim)
gen tempSafeHarbor = max(tempExcessAPTC - tempAPTC_repay, 0)
replace APTC = annAPTC_f1095A if APTC == . & irmf == 1
replace SLCSP = annSLCSP_f1095A if SLCSP == . & irmf == 1
gen safeHarbor = max(excessAPTC - APTC_repay, 0)
tabstat safeHarbor [aw=wt] if flpdyr == 2019, stat(sum) format(%12.0f)

gen nonFilerAPTC = 0
replace nonFilerAPTC = APTC if irmf == 1 & f8962 == 0
replace APTC = annAPTC_f1095A if nonRecon == 1 | neverFiler == 1
replace APTC = aptc_13 if nonRecon == 1 & APTC == .
replace PTC = tempPTC if nonRecon == 1
replace excessAPTC = tempExcessAPTC if nonRecon == 1
replace repayLimit = calc_repayLim if nonRecon == 1 | neverFiler == 1
replace safeHarbor = tempSafeHarbor if nonRecon == 1 | neverFiler == 1

* No true up or repayment for neverFilers
gen nonReconTrueUp = tempTrueUp if nonRecon == 1 | neverFiler == 1
gen nonReconAPTC_repay = tempAPTC_repay if nonRecon == 1 | neverFiler == 1

* "Compliant" APTC
gen APTC_comp = APTC
replace APTC_comp = APTC_comp - nonReconAPTC_repay if nonRecon == 1

replace safeHarbor = nonReconAPTC_repay + safeHarbor if flpdyr == 2020 & (nonRecon == 1 | neverFiler == 1)
replace nonReconAPTC_repay = 0 if flpdyr == 2020
drop temp*

* mars = 1: single
* mars = 2: married, joint
* mars = 3: married, separate (no spousal exemption)
* mars = 4: HoH
* mars = 5: widow(er) with dependent child
* mars = 6: married, separate (claiming spousal exemption)

gen single = (famSize == 1) | (mars == 1)
gen fam_0 = (famSize == 2) & (mars == 2)
gen fam_1 = (famSize == 3) & (mars == 2)
gen fam_2 = (famSize == 4) & (mars == 2) 
gen fam_3 = (famSize > 4) & (mars == 2) 
gen other = (mars > 2)
gen cp = (famSize == 2)
gen fam = (famSize > 2)

gen famType = 1 if ((famSize == 1) | (mars == 1))
replace famType = 2 if ((famSize == 2) & (mars == 2)) & famType == .
replace famType = 3 if ((famSize == 3) & (mars == 2)) & famType == .
replace famType = 4 if ((famSize == 4) & (mars == 2)) & famType == .
replace famType = 5 if ((famSize > 4) & (mars == 2)) & famType == .
replace famType = 6 if (mars > 2) & famType == .

* Make Figure 2: by tax year, total PTC components
do "$workingDir/Figure_2_total_PTC_components.do"

* Make Figure 3C series: average APTC and PTC by AGI, as a replacement for a defined PTC "schedule" as with other credits
do "$workingDir/Figure_3C_avg_PTC_schedule.do"

* Make Figure 3B:
do "$workingDir/Figure_3B_PTC_amts_ratios.do"

* Make Figure 3D/4A:
do "$workingDir/Figure_3D_4A_PTC_comp_distro.do"
